package cn.zclong.publish.dao;


import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.jdbc.TxQueryRunner;
import cn.zclong.article.domain.Article;

public class PublishDao {

	private QueryRunner qr = new TxQueryRunner();
	 
	private final Integer integral = 50; //一篇新文章加50积分

	public String publish(Article article) {
		try{
			String sql = "insert into article(id,title,readnum,time,message,uid) values(?,?,?,?,?,?)";
			//向数据库中插入文章信息
			qr.update(sql,article.getId(),article.getTitle(),article.getReadnum(),
					article.getTime(),article.getMessage(),article.getUid());
			
			
			//新增文章之后，给用户加积分		
			sql = "select integral from t_user where uid=?";
			long i = Long.parseLong(""+qr.query(sql, new ScalarHandler(), article.getUid()));
			i = i+integral;
			
			//修改积分
			sql =  "update t_user set integral =? where uid=?";
			qr.update(sql, i, article.getUid());
			
			//返回文章id
			return article.getId();
		}catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
}
